package com.example.hszpad;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends BaseAdapter {
    private Context context;
    private List registers = new ArrayList();

    public MyAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return registers.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_list_item, null);
            holder = new ViewHolder();
            holder.tv_data = convertView.findViewById(R.id.tv_data);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_data.setText(String.format("%02d : %d", position, registers.get(position)));
        return convertView;
    }

    static class ViewHolder {
        TextView tv_data;
    }

    public void refresh(int[] data) {
        registers.clear();
        for (int d : data) {
            registers.add(d);
        }
        notifyDataSetChanged();
    }
}
